package com.thti.mecool.client.activity;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.threeti.mecool.mobile.interfaces.dto.ProjectStoreDto;
import com.thti.mecool.client.adapter.MakePlanSelectTerminalAdapter;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.model.biz.ProjectServerBizImpl;
import com.thti.mecool.client.model.po.ProjectServer;
import com.thti.mecool.client.widget.Toolbar;

public class MakePlanSelectTerminalActivity extends BaseActivity {
	private ListView listView;
	private Context mContext;
	private String curProjectId;
	private ProjectStoreDto projectStoreDto;
	private HashMap<Integer, String> ProjectStores = new HashMap<Integer, String>();
	private HashMap<Integer, String> ProjectStoreIds = new HashMap<Integer, String>();
	private MakePlanSelectTerminalAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.noTitle();
		setContentView(R.layout.mecool_make_plan_select_terminal);
		Cache.add(this);
		initToolbar();
		curProjectId = getIntent().getStringExtra("curProjectId");
		init();
		initSql();
		service();
	}

	private void initSql() {
		ProjectServerBizImpl projectServerBizImpl = new ProjectServerBizImpl(
				this);
		List<ProjectServer> projectServerList = projectServerBizImpl
				.findBystore(curProjectId, 2);
		if (projectServerList != null & projectServerList.size() > 0) {
			int i = 0;
			for (ProjectServer ps : projectServerList) {
				ProjectStores.put(i, ps.getName());
				ProjectStoreIds.put(i, ps.getStoreId());
				i++;
			}
			service();
		}
	}

	private void init() {
		listView = (ListView) findViewById(R.id.mecool_make_plan_select_terminal_listview);
		mContext = MakePlanSelectTerminalActivity.this;
	}

	public void initToolbar() {
		Toolbar toolbar = new Toolbar(this);
		toolbar.setToolbarCentreText("制定计划-选择终端");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
		// toolbar.right_btn.setOnClickListener(rightbuttonListener);
	}

	private OnClickListener leftbuttonListener = new ImageButton.OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}
	};

	private OnClickListener rightbuttonListener = new ImageButton.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

		}

	};

	OnItemClickListener listListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("ProjectStoreName", ProjectStores.get(arg2));
			bundle.putString("ProjectStoreId", ProjectStoreIds.get(arg2));
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			finish();
		}
	};

	private void service() {
		if (getDataFlag(HandlerConstant.PlanCommand)) {
			initSql();
		}
		if (adapter == null) {
			adapter = new MakePlanSelectTerminalAdapter(mContext, ProjectStores);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(listListener);
		} else {
			adapter.notifyDataSetChanged();
		}
	}
}
